
package com.tools.cleanmaster.bean;

import android.graphics.drawable.Drawable;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.appinfo.AppManager;
import com.tools.cleanmaster.model.item.ApkFileItem;
import com.tools.cleanmaster.appinfo.AppInfoSnapshot;

import java.io.File;
import java.lang.ref.SoftReference;

/**
 * 存储APK文件相关信息，用来生成ApkFileItem
 * 
 * @author ouyang
 */
public class ApkFileInfo {
    public String filePath;

    public long size;

    public String pkgName;

    public int versionCode;

    public long apkCreateTime;

    public String versionName;

    public String appName;

    public int flag;

    /** 应用图标 */
    private SoftReference<Drawable> appIcon;

    /**
     * @return 转化生成ApkFileItem
     */
    public ApkFileItem createApkFileItem() {
        if (filePath == null) {
            return null;
        }
        File file = new File(filePath);
        if (!file.exists() || !file.isFile()) {
            return null;
        }
        ApkFileItem item = new ApkFileItem();
        item.filePath = filePath;
        item.isDir = false;
        item.flag = flag;
        item.versionCode = versionCode;
        item.versionName = versionName;
        item.pkgName = pkgName;
        item.appName = appName;
        item.apkCreateTime = apkCreateTime;
        item.fileCount = 1;
        item.size = size;
        item.isSelectedDefault  = item.isSelected = false;
        if (pkgName != null) {
            AppInfoSnapshot appInfo = AppManager.getInstance(CleanApplication.getInstance()).getAppInfo(
                    pkgName);
            if (appInfo != null) {
                item.flag = ApkFileItem.FLAG_INSTALLED;
                item.isSelectedDefault = item.isSelected = true;
            }
        }
        return item;
    }
}
